﻿using FarmSmartSleep.Common.Domain.RBACFile;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using R.FarmSmartSleep.RBAC.API.Common.R.Dtos;

namespace R.FarmSmartSleep.RBAC.API.Apliction.Command.Permissions
{
    /// <summary>
    /// 角色权限显示列表
    /// </summary>
    public class PermissionListHandler : IRequestHandler<PermissionListCommon, ResultPage<RolesPermissionListDto>>
    {
        private readonly IBaseRepository<Roles> rolesRep;
        private readonly IBaseRepository<Permission> permissionRep;
        private readonly IBaseRepository<RolesPermissions> rolesPermissionRep;
        /// <summary>
        /// 权限显示列表
        /// </summary>
        /// <param name="rolesRep"></param>
        /// <param name="permissionRep"></param>
        public PermissionListHandler(IBaseRepository<Roles> rolesRep, IBaseRepository<Permission> permissionRep, IBaseRepository<RolesPermissions> rolesPermissionRep)
        {
            this.rolesRep = rolesRep;
            this.permissionRep = permissionRep;
            this.rolesPermissionRep = rolesPermissionRep;
        }
        /// <summary>
        /// 以权限为主的显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultPage<RolesPermissionListDto>> Handle(PermissionListCommon request, CancellationToken cancellationToken)
        {
             var result = new ResultPage<RolesPermissionListDto>() { Code=(int)CodeEnum.search};
            try
            {
                var roles =  rolesRep.GetAll().ToList();
                var permission =  permissionRep.GetAll().ToList();
                var rolesPermission =  rolesPermissionRep.GetAll().ToList();
                var list = (from r in roles
                            join rp in rolesPermission
                            on r.RolesId equals rp.RolesId
                            join p in permission
                            on rp.PermissionsId equals p.PermissionsId
                            group r by new { p.PermissionsId, p.PermissionsName, p.CanView,p.CanAdd,p.CanEdit,p.CanDelete } into g
                            select new RolesPermissionListDto
                            {
                                PermissionsId = g.Key.PermissionsId,
                                PermissionsName = g.Key.PermissionsName,
                                RoleName = string.Join(",", g.Select(p => p.RoleName)),
                                CanView = g.Key.CanView,
                                CanAdd = g.Key.CanAdd,
                                CanEdit = g.Key.CanEdit,
                                CanDelete = g.Key.CanDelete,
                            }).ToList();
                result.totalCount = list.Count();
                result.totalPage = (int)Math.Ceiling(list.Count() / (double)request.pageSize);
                list = list.Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
                result.Data = list;
                 
            }
            catch (Exception)
            {
                result.Messege = "查询失败";
                throw;
            }
            return result;
        }
    }
}
